<%--
*
* 作者： Sky
*
* 描述： 选择树
*
--%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="com.sky.common.string.StringUtils"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>选择树</title>
	<%@include file="/share/header.jsp"%>
	<%@include file="/share/DhmlxTreeheader.jsp"%>
</head>
	<%
		String type=request.getParameter("type");//树类型
		String returnIdName=request.getParameter("returnIdName");//返回结果ID名称
		String returnTextName=request.getParameter("returnTextName");//返回TEXT结果名称
		String rootIds=request.getParameter("rootIds");//根节点
		String excludeIds=request.getParameter("excludeIds");//排除节点
		String level=request.getParameter("level");//展开层数	
		boolean checkbox=Boolean.parseBoolean(request.getParameter("checkbox"));//是否checkbox
		String checkedValues=request.getParameter("checkedValues");//默认选中的值
		boolean threeState=Boolean.parseBoolean(request.getParameter("threeState"));//是否三态
		boolean require=Boolean.parseBoolean(request.getParameter("require"));//是否不允许为空
		if(StringUtils.isEmpty(type)){
			throw new Exception("未设置树【" + type + "】，请与系统管理员联系!");
		}
		if(StringUtils.isEmpty(returnIdName)){
			throw new Exception("未设置返回ID结果名称，请与系统管理员联系!");
		}
		if(StringUtils.isEmpty(returnTextName)){
			throw new Exception("未设置返回TEXT结果名称，请与系统管理员联系!");
		}				
	%>	
	<body scroll="no" style="margin:0;padding: 0;border: 0;">
		<div align="center"  style="margin:0;padding: 0;border: 0;">
			<p  style="margin:5px;">
				<a href="javascript:tree.openAllItems(0);">全部展开</a> 
				| 
				<a href="javascript: tree.closeAllItems(0);">全部收缩</a>
				<br>
			</p>
		</div>
		<div id="treeboxbox_tree" align="left" style="height: 100%;"></div>
		<script> 
            tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);//创建树对象
            tree.setImagePath("<%=path%>/dhtmlxtree/imgs/csh_bluebooks/");//设置树图标路径
            tree.setXMLAutoLoading("<%=path%>/share/treeData.jsp?type=<%=type%>&level=<%=level%>&rootIds=<%=rootIds%>&excludeIds=<%=excludeIds%>"); //设置数据路径
            tree.setDataMode("json");//设置数据类型
            tree.enableCheckBoxes(<%=checkbox%>);//启用checkbox
            tree.enableThreeStateCheckboxes(<%=threeState%>);//启用三态checkbox
            tree.setOnClickHandler(onNodeSelect);//set function object to call on node select
            tree.loadJSON("<%=path%>/share/treeData.jsp?type=<%=type%>&level=<%=level%>&rootIds=<%=rootIds%>&excludeIds=<%=excludeIds%>");//加载第一级节点
            tree.attachEvent("onXLE",setCheckedValue);//加载完树后选中
            tree.attachEvent("onDblClick", function(id){});//屏蔽双击事件
            document.getElementById("treeboxbox_tree").style.overflowX="hidden";//设置滚动条
            document.getElementById("treeboxbox_tree").style.overflowY="auto";//设置滚动条
           	/*
             *设置checked的值为选中状态
            */                      
            function setCheckedValue(){ 
            	if(<%=checkbox%> && <%=!StringUtils.isEmpty(checkedValues)%>){
	            	//alert("<%=checkedValues%>");
	            	var checkedItemIds="<%=checkedValues%>".split(",");
	            	for(var i=0;i<checkedItemIds.length;i++){
	            		tree.setCheck(checkedItemIds[i],true);	
	            	}
            	}
				$(document.body).height($(frameElement).height()-12-12); 
            }  
			//选中执行的事件           
            function onNodeSelect(nodeId){
               //alert(tree.getOpenState(nodeId));
               if(tree.getOpenState(nodeId)==1){
               		tree.closeItem(nodeId);
               }else{
               		tree.openItem(nodeId);
               }
            }            
           //选择
            function onDoSelect(parameters){
	             var selectedItemId;//获取选中的值
	             var selectedItemText;
	             if(<%=checkbox%>){
	             	//多选
	             	selectedItemId=tree.getAllChecked();//获取选中的值
					var selectedItemIds=selectedItemId.split(',');
					var selectedItemArray=new Array();
					for(var i=0;i<selectedItemIds.length;i++){
						selectedItemArray[i]=tree.getItemText(selectedItemIds[i]);
					}
					selectedItemText=selectedItemArray.toString();
	             }else{
	             	//单选
	             	selectedItemId=tree.getSelectedItemId();//获取选中的值
	            	selectedItemText=tree.getItemText(tree.getSelectedItemId());
	             }
	             //alert("ID:"+selectedItemId+"\tTEXT:"+selectedItemText);
	             if(selectedItemId==null||selectedItemId==""||selectedItemText==null||selectedItemText==""){
					 if(<%=!require%>){//非必选    
						top.MyWindow.confirm({
							content : '你还没有选择，不选择则会清空内容，确实不选择吗？',
							title : '确认框',
							container : top,
							callback :function(action){
									　　　if(action == 'ok'){
									                 selectedItemId="";
									                 selectedItemText="";
									                 parameters.source.document.getElementsByName("<%=returnIdName%>")[0].value=selectedItemId;
										             parameters.source.document.getElementsByName("<%=returnTextName%>")[0].value=selectedItemText;
										             parameters.source.document.getElementsByName("<%=returnTextName%>")[0].focus();
									            	 top.MyWindow.close(parameters.id,parameters.container);	
										             
									　　　}
									  }
									});
							return false;
		              }else{
							top.MyWindow.tips({
								content : '请选择!',
								title : '提示',
								type : 'alert',
								container : top					
							});			             	
			             	return false;
		             }
	             }else{
		             //alert("returnIdName:"+parameters.source.document.getElementsByName("<%=returnIdName%>").length+"\treturnTextName:"+parameters.source.document.getElementsByName("<%=returnTextName%>").length);
		             if(parameters.source.document.getElementsByName("<%=returnIdName%>").length==0){
		             	alert("未找到返回结果ID的名称【<%=returnIdName%>】，请与系统管理员联系！");
		             	return true;
		             }
		             if(parameters.source.document.getElementsByName("<%=returnTextName%>").length==0){
		             	alert("未找到返回结果TEXT的名称【<%=returnTextName%>】，请与系统管理员联系！");
		             	return true;
		             }	             
		             parameters.source.document.getElementsByName("<%=returnIdName%>")[0].value=selectedItemId;
		             parameters.source.document.getElementsByName("<%=returnTextName%>")[0].value=selectedItemText;
		             parameters.source.document.getElementsByName("<%=returnTextName%>")[0].focus();
		             return true;
	             }
            }          
        </script>
	</body>
</html>
